
import java.util.Scanner;

public class Test{
	public static void main(String[] args) {

		//System.out.println("Hello World");

		//最大子数组之和
//		Scanner scanner = new Scanner(System.in);
//
//		System.out.println("请输入数组的长度：");
//		int length = scanner.nextInt();
//		int[] nums = new int[length];
//
//		System.out.println("请输入" + length + "个数组元素：");
//		for (int i = 0; i < length; i++) {
//			nums[i] = scanner.nextInt();
//		}
//
//		int maxSum = MaxSubArraySum.maxSubArray(nums);
//		System.out.println("子数组之和的最大值为：" + maxSum);
//
//		scanner.close();



		//输出1~20000的所有素数
		// 记录开始时间
		long startTime = System.nanoTime();

		int count = 0;
		for (int number = 2; number <= 20000; number++) {
			if (!isPrime(number)) {
				System.out.print(number + " ");
				count++;
				if (count % 5 == 0) {
					System.out.println();
				}
			}
		}
		// 记录结束时间
		long endTime = System.nanoTime();

		// 计算执行时间，并将其转换为毫秒
		long duration = (endTime - startTime) / 1_000_000; // 除以1,000,000,000（10^9）将纳秒转换为秒
		// 输出执行时间



		//埃拉托斯特尼筛法
		long startTime2 = System.nanoTime();
		boolean[] prime2 = new boolean[20001];
		for (int i = 2; i * i <= 20000; i++) {
			if (prime2[i]) continue;
			for (int j = i * i; j <= 20000; j += i) {
				prime2[j] = true;
			}
		}

		int count2 = 0;
		for (int number = 2; number <= 20000; number++) {
			if (!prime2[number]) {
				System.out.print(number + " ");
				count++;
				if (count2 % 5 == 0) {
					System.out.println();
				}
			}
		}
		long endTime2 = System.nanoTime();
		long duration2 = (endTime2 - startTime2) / 1_000_000;

		System.out.println("程序1执行时间（毫秒）: " + duration);
		System.out.println("程序2执行时间（毫秒）：" + duration2);

	}








	public static boolean isPrime(int number) {
		if (number <= 1) return false;
		for (int i = 2; i * i <= number; i++) {
			if (number % i == 0) {
				return false;
			}
		}
		return true;
	}
}

